package tt.dz.egobus.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.annotation.LogAuto;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.api.managerservice.manager.*;
import tt.dz.egobus.bean.Factory;
import tt.dz.egobus.bean.Pagination;
import tt.dz.egobus.bean.ServiceManagerI18NCodeEnum;
import tt.dz.egobus.cache.service.ExtendCacheService;
import tt.dz.egobus.dubbo.annotation.Interceptor;
import tt.dz.egobus.servicemanager.bean.SpringConfigConstant;
import tt.dz.egobus.servicemanager.business.distance.PointFilter;
import tt.dz.egobus.servicemanager.business.filter.BespeakBusPointMatchHandler;
import tt.dz.egobus.servicemanager.business.filter.BusPointMatchHandler;
import tt.dz.egobus.servicemanager.business.filter.RightNowBusPointMatchHandler;
import tt.dz.egobus.servicemanager.business.key.CodeBusinessIdGenerator;
import tt.dz.egobus.servicemanager.business.key.DriverStatus;
import tt.dz.egobus.servicemanager.business.lock.BusinessLock;
import tt.dz.egobus.servicemanager.business.lock.BusinessRedisLock;
import tt.dz.egobus.servicemanager.business.point.AddressPoint;
import tt.dz.egobus.servicemanager.business.point.BusPoint;
import tt.dz.egobus.servicemanager.business.task.UserRequestGrabBusinessDynamicInitTask;
import tt.dz.egobus.servicemanager.core.task.DynamicTaskManager;
import tt.dz.egobus.servicemanager.entities.BusinessService;
import tt.dz.egobus.servicemanager.entities.BusinessServiceInfo;
import tt.dz.egobus.servicemanager.entities.DriverRegisterBusRelative;
import tt.dz.egobus.servicemanager.facade.common.CommonFacade;
import tt.dz.egobus.servicemanager.facade.order.OrderFacade;
import tt.dz.egobus.servicemanager.service.*;
import tt.dz.egobus.servicemanager.util.PojoConvertVoUtils;
import tt.dz.egobus.servicemanager.vo.BusinessFinishVo;
import tt.dz.egobus.type.LogType;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

import static tt.dz.egobus.api.util.ResponseHelper.copy;
import static tt.dz.egobus.bean.I18NCodeEnum.*;
import static tt.dz.egobus.bean.ServiceManagerI18NCodeEnum.*;

/**
 * @author Anson
 * @date 17-8-8
 * @name 抢单API实现类
 * @since 1.0.6
 */
@Service(version = "1.0.6", interfaceClass = GrabBusinessApiService.class)
public class GrabBusinessApiServiceImpl extends ManagerApiServiceImpl implements GrabBusinessApiService {

    private static final Logger LOGGER = LoggerFactory.getLogger(GrabBusinessApiServiceImpl.class);


    /**
     * @param busId    车id
     * @param driverId 司机id
     * @return 当前是否激活成功
     * @throws ResponseException 司机与车找不到对应的对象的时候
     * @author Anson
     * @date 17-3-15
     * @name 根据司机id与车辆id进行业务上的激活
     * @since 1.0.2
     */
    @Override
    public ResponseBack<Boolean> active(String busId, String driverId) throws ResponseException {
        GrabManagerService grabManagerService = getGrabManagerService();
        return grabManagerService.active(busId, driverId);
    }

    /**
     * @param requestTask 请求参数配置
     * @throws ResponseException 非法的算法名称以及非法的业务编号
     * @author Anson
     * @date 17-3-24
     * @name 根据当前请求的点的坐标请求派单任务
     * @desc 当前采用抢单模式, 即在一定范围内, 存在司机最优队列, 则先推送派单到司机端,
     * 然后司机端进行抢单
     * @see DistributeDriverTask#getPointVos()
     * 搜索最优队列 当前队列按照最近原则进行了排序，即第一个为最近的点
     * 司机状态从激活 -> 抢单
     * @since 1.0.6
     */
    @Interceptor
    @LogAuto(bizCode = 20090501, bizDesc = "抢单模式用户请求派单", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<DistributeDriverTask> requestTask(RequestTask requestTask) throws ResponseException {
        try {
            LOGGER.info("抢单模式用户请求派单->方法开始", requestTask);
            this.validateRequest(requestTask);
            String userId = requestTask.getUserId();
            int businessCode = requestTask.getBusinessCode();
            LOGGER.info("抢单模式用户请求派单->当前获取用户={}", userId);
            SpringConfigConstant springConfigConstant = this.getSpringConfigConstant();
            GrabBusinessCacheService businessCacheService = this.getGrabBusinessCacheService();
            ThreadPoolExecutor executor = getThreadPoolExecutor();
            DynamicTaskManager dynamicTaskManager = this.getDynamicTaskManager();
            DynamicTaskService dynamicTaskService = this.getDynamicTaskService();
            OrderFacade orderFacade = this.getOrderFacade();
            ResponseBack<Boolean> businessVerify = orderFacade.verifyOrderServeNumber(businessCode);
            //验证businessCode是否合法
            if ((!businessVerify.isSuccess() && !businessVerify.getResult())) {
                return ResponseBack.error(this.get(RPC_INVALIDATE_BUSINESS_NO_ERROR));
            }
            if (UserRequestTypeInfo.UserRequestType.indexOf(requestTask.getUserRequestInfo().getType()) ==
                    UserRequestTypeInfo.UserRequestType.BESPEAK) {
                UserRequestTypeInfo userRequestTypeInfo = requestTask.getUserRequestInfo();
                //验证预约单的必要请求参数
                //预约的最短时间间隔
                Long minRequestLimitSeconds = springConfigConstant.userRequestBespeakMinLimitSeconds();
                //预约的最长时间间隔
                Long userRequestBespeakMaxLimitSeconds = springConfigConstant.userRequestBespeakMaxLimitSeconds();
                long createTime = userRequestTypeInfo.getCreateTime();
                long bespeakTime = userRequestTypeInfo.getBespeakTime();
                //创建时间 + 最小限制秒数*1000 > 预约时间点 则认为 不满足最小间隔数
                if (createTime + minRequestLimitSeconds * 1000 > bespeakTime) {
                    LOGGER.info("抢单模式用户请求派单->预约时间间隔过短,createTime={},bespeakTime={},min={}", createTime, bespeakTime,
                            minRequestLimitSeconds);
                    return ResponseBack.error(this.get(RPC_USER_BESPEAK_TIME_NO_MATCHE_MIN_LIMIT_ERROR));
                }
                //创建时间 + 最小限制秒数*1000 < 预约时间点 则认为 不满足最长间隔数
                if (createTime + userRequestBespeakMaxLimitSeconds * 1000 < bespeakTime) {
                    LOGGER.info("抢单模式用户请求派单->预约时间间隔过长,createTime={},bespeakTime={},max={}", createTime, bespeakTime,
                            userRequestBespeakMaxLimitSeconds);
                    return ResponseBack.error(this.get(RPC_USER_BESPEAK_TIME_NO_MATCHE_MAX_LIMIT_ERROR));
                }
            }
            //车辆匹配器
            BusPointMatchHandler matchHandler = this.getRequestPointFilter(requestTask);
            BusPoint[] busPoints = matchHandler.filter(null);
            if (busPoints == null || busPoints.length == 0) {
                //当前无单可派的时候,范湖车辆繁忙中
                return ResponseBack.error(this.get(RPC_BUS_BUSY));
            }
            LOGGER.info("抢单模式用户请求派单->搜索到可用的司机个数={}", ArrayUtils.getLength(busPoints));
            //司机抢单超时时间单位秒
            Long driverGrabTimeoutSeconds = springConfigConstant.driverBusinessGrabTimeout();
            //用户请求派单时间超时时间单位秒
            Long userRequestTimeoutSeconds = springConfigConstant.userBusinessBusRequestTimeout();
            DistributeDriverTask distributeDriverTask = new DistributeDriverTask();
            //业务id生成器
            Factory<String> businessGenerator = new CodeBusinessIdGenerator(businessCode);
            //业务流水号生成器
            String businessId = businessGenerator.facorty();
            distributeDriverTask.setBusinessId(businessId);
            distributeDriverTask.setUserId(userId);
            distributeDriverTask.setGrabLimitTimeoutSeconds(driverGrabTimeoutSeconds);
            distributeDriverTask.setLimitTimeoutSeconds(userRequestTimeoutSeconds);
            distributeDriverTask.setBusinessCreateTime(System.currentTimeMillis());
            if (busPoints != null && busPoints.length > 0) {
                GrabManagerService managerService = getGrabManagerService();
                //处理抢单队列结果
                distributeDriverTask.setPointVos(copy(busPoints, BusPointVo.class));
                //添加当前业务请求,并设置其状态为抢单状态
                businessCacheService.updateBusiness(requestTask, distributeDriverTask, busPoints);
                //设置当前司机状态为抢单状态,并且绑定当前司机的业务流水号
                managerService.handlerDriversGrabStatus(busPoints, businessId);
                //添加异步事务,添加用户请求派单时候,系统新增的定时任务
                executor.execute(new UserRequestGrabBusinessDynamicInitTask(businessId, businessCacheService,
                        dynamicTaskManager, dynamicTaskService));
            }
            return ResponseBack.success(distributeDriverTask);
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("抢单模式用户请求派单->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    /**
     * @param requestTask
     * @author Anson
     * @date 17-8-29
     * @name 不同场景下坐标过滤的匹配器
     * @since 1.0.7
     */
    private BusPointMatchHandler getRequestPointFilter(RequestTask requestTask) {
        BusPointMatchHandler matchHandler = null;
        SpringConfigConstant springConfigConstant = this.getSpringConfigConstant();
        UserRequestTypeInfo info = requestTask.getUserRequestInfo();
        GrabRedisBusinessWorkCacheService grabRedisBusinessWorkCacheService = getGrabRedisBusinessWorkCacheService();
        UserRequestTypeInfo.UserRequestType userRequestType = UserRequestTypeInfo.UserRequestType.indexOf(info
                .getType());
        switch (userRequestType) {
            case NOW:
                matchHandler = new RightNowBusPointMatchHandler(grabRedisBusinessWorkCacheService, requestTask,
                        springConfigConstant);
                break;
            case BESPEAK:
                matchHandler = new BespeakBusPointMatchHandler(grabRedisBusinessWorkCacheService, requestTask);
                break;
            default:
                matchHandler = new BusPointMatchHandler() {
                    @Override
                    public BusPoint[] filter(PointFilter filter) {
                        return new BusPoint[0];
                    }
                };
                break;
        }
        return matchHandler;
    }

    /**
     * @param driverId   当前司机的id
     * @param businessId 当前业务请求id
     * @return 返回是否抢单成功
     * ${@linkplain ResponseBack#getResult()} true：b绑定成功,false：绑定失败
     * @throws ResponseException 请求或者driver主键查询为空
     * @author Anson
     * @date 17-3-24
     * @name 司机接受进行某个抢单
     * <p>
     * 若当前抢单成功,则会进行一下处理 :
     * 司机当前 状态从抢单中-->开始
     * <p>
     * 当前业务逻辑会校验当前司机的相关状态以及是否存在有效的绑定关系等等
     * 若司机抢单不成功则 :
     * 司机状态由 抢单中 --> 激活
     * 推送当前业务到用户端
     * @since 1.0.6
     */
    @Interceptor
    @LogAuto(bizCode = 20090502, bizDesc = "抢单模式司机抢单确认", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> driverAcceptAndBeginTask(String driverId, String businessId) throws ResponseException {
        try {
            if (StringUtils.isEmpty(driverId) || StringUtils.isEmpty(businessId)) {
                throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
            }
            ExtendCacheService extendCacheService = this.getExtendCacheService();
            BusinessLock businessLock = new BusinessRedisLock(extendCacheService);
            try {
                if (businessLock.aquireLock()) {
                    GrabManagerService managerService = getGrabManagerService();
                    return managerService.driverGrabAcceptAndBeginTask(driverId, businessId);
                }
                return ResponseBack.error(this.get(RPC_DRIVER_GRAB_FAIL));
            } finally {
                businessLock.unlock();
            }
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("抢单模式司机抢单确认->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    /**
     * @param userId     用户id
     * @param businessId 业务id
     * @author Anson
     * @date 17-8-7
     * @name 用户取消抢单流程
     * @desc 用户在未接收到真正订单时候, 可取消当前抢单流程
     * @since 1.0.6
     */
    @Interceptor
    @LogAuto(bizCode = 20090503, bizDesc = "抢单模式用户取消抢单", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<DistributeDriverTask> userCancelGrabBusiness(String userId, String businessId) throws
            ResponseException {
        if (StringUtils.isAnyEmpty(userId, businessId)) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        return this.getGrabManagerService().userCancelGrabBusiness(userId, businessId);
    }

    @Override
    public ResponseBack<BusinessView> getBusinessViewByOrder(String orderId) throws ResponseException {
        if (StringUtils.isAnyEmpty(orderId)) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        return this.getGrabManagerService().getBusinessViewByOrder(orderId);
    }


    @Interceptor
    @LogAuto(bizCode = 20090504, bizDesc = "抢单模式用户完成", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<BusinessVo> driverFinish(BusinessDetail detail) throws ResponseException {
        {
            try {
                this.validateRequest(detail);
                GrabRedisBusinessWorkCacheService businessWorkCacheService = getGrabRedisBusinessWorkCacheService();
                GrabManagerService managerService = getGrabManagerService();
                String driverId = detail.getDriverId();
                LOGGER.info("抢单模式抢单完成->当前获取司机={}", driverId);
                DriverStatus driverStatus = businessWorkCacheService.getStatus(driverId);
                //确认当前司机流程为business，即接单任务中状态
                if (driverStatus == null || (!DriverRegisterBusRelative.RelativeStatus.assertBusiness(driverStatus
                        .getStatus()))) {
                    LOGGER.error("抢单模式抢单完成->当前司机状态异常,driverId={},异常原因:status={}", driverId, driverStatus == null ?
                            "未绑定关系" : "不处于业务流程中");
                    ServiceManagerI18NCodeEnum i18NCodeEnum = driverStatus == null ?
                            RPC_BUS_DRIVER_RELATIVE_NULL_BIND : RPC_DRIVER_NOT_IN_BUSINESS_ERROR;
                    throw new ResponseException(i18NCodeEnum);
                }
                BusPoint busPoint = businessWorkCacheService.getNowPoint(driverId);
                if (busPoint == null) {
                    //当前请求车辆地理位置为空
                    throw new ResponseException(RPC_NOT_FOUND_DRIVER_UPLOAD_POINT_ERROR);
                }
                LOGGER.info("抢单模式抢单完成->当前获取司机的地理位置为{}", busPoint);
                BusinessFinishVo vo = PojoConvertVoUtils.businessDetailConvertBusinessFinishVo(detail);
                if (vo == null || !vo.validate()) {
                    LOGGER.error("抢单模式抢单完成->请求参数转换业务参数验证失败:原始={},转换:={}", detail, vo);
                    throw new ResponseException(RPC_SYSTEM_CODE_ERROR);
                }
                //数据库数据操作,并并生成订单信息
                ResponseBack<BusinessServiceInfo> responseBack = managerService.doFinish(vo);
                if (responseBack.isSuccess()) {
                    BusinessServiceInfo info = responseBack.getResult();
                    LOGGER.info("抢单模式抢单完成->获取当前账单详细信息完成={}", info.getBusinessId());
                    return ResponseBack.success(managerService.convertInfoToBusinessVo(info));
                } else {
                    LOGGER.error("抢单模式抢单完成->流程详细信息未正确完成businessId={}", detail.getBusinessId());
                    return new ResponseBack(responseBack.getCode(), responseBack.getMsg());
                }
            } catch (ResponseException e) {
                return ResponseBack.error(e);
            } catch (RuntimeException e) {
                LOGGER.error("抢单模式抢单完成->系统异常", e);
                return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
            }
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090505, bizDesc = "抢单模式用户取消行程", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> userCancel(String userId, String businessId) throws ResponseException {
        try {
            if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(businessId)) {
                throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
            }
            LOGGER.info("用户取消行程->当前用户={},业务信息={}", userId, businessId);
            GrabManagerService managerService = getGrabManagerService();
            //数据库更新流水信息
            ResponseBack<Boolean> isDoneResponse = managerService.doUserCancel(userId, businessId);
            LOGGER.info("用户取消行程->修改数据库取消的状态={},businessId={}", isDoneResponse.isSuccess(), businessId);
            return isDoneResponse;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("用户取消行程->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    /**
     * @param request
     * @author Anson
     * @date 17-8-7
     * @name 抢单模式司机开始行程
     * @desc 用户在未接收到真正订单时候, 可取消当前抢单流程
     * @since 1.0.6
     */
    @Interceptor
    @LogAuto(bizCode = 20090506, bizDesc = "抢单模式司机开始行程", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> driverDoTask(DriverDoTaskRequest request) throws ResponseException {
        try {
            LOGGER.info("司机确认身份之后触发->方法开始");
            this.validateRequest(request);
            String driverId = request.getDriverId();
            String businessId = request.getBusinessId();
            AddressPoint addressPoint = PojoConvertVoUtils.driverDoTaskRequestConvetAddressPoint(request);
            LOGGER.info("司机确认身份之后触发->当前获取司机={}", driverId);
            GrabManagerService managerService = getGrabManagerService();
            BusinessServiceService businessServiceService = this.getBusinessService();
            GrabRedisBusinessWorkCacheService businessWorkCacheService = getGrabRedisBusinessWorkCacheService();
            BusinessService businessService = businessServiceService.getBusinessByBusinessId(businessId);
            if (businessServiceService == null) {
                //当前的业务流水号不存在的时候
                throw new ResponseException(RPC_PARAM_ENTITY_ERROR);
            }
            DriverStatus driverStatus = businessWorkCacheService.getStatus(driverId);
            //确认当前司机流程为doBusiness，即接单任务准备中状态
            if (driverStatus == null || !DriverRegisterBusRelative.RelativeStatus.assertBeginBusiness(driverStatus
                    .getStatus())) {
                throw new ResponseException(RPC_DRIVER_NOT_IN_BEGIN_BUSINESS_ERROR);
            }
            LOGGER.info("司机确认身份之后->上传的地理位置为={}", addressPoint);
            //数据库数据操作
            ResponseBack<Boolean> responseBack = managerService.doBusiness(driverId, businessService, addressPoint);
            LOGGER.info("司机确认身份之后->修改数据状态={},businessId={}", responseBack.isSuccess(), businessId);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("司机确认身份之后->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Override
    public ResponseBack<DistributeDriverTask> getGrabBusinessDistributeDriverTask(String businessId) throws
            ResponseException {
        if (StringUtils.isAnyEmpty(businessId)) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        return this.getGrabManagerService().getGrabBusinessDistributeDriverTask(businessId);
    }

    @Interceptor
    @LogAuto(bizCode = 20090507, bizDesc = "抢单模式司机放弃抢单", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> driverGiveUpGrabTask(String driverId, String businessId) throws ResponseException {
        if (StringUtils.isAnyEmpty(driverId, businessId)) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        return this.getGrabManagerService().driverGiveUpGrabTask(driverId, businessId);
    }

    @Interceptor
    @LogAuto(bizCode = 20090508, bizDesc = "抢单模式获取业务基本限制状态", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<BusinessBaseLimitStatus> accBusinessStatus(String businessId) throws ResponseException {
        if (StringUtils.isEmpty(businessId)) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        BusinessBaseLimitStatus businessBaseLimitStatus = getGrabManagerService().accBusinessStatus(businessId);
        return ResponseBack.success(businessBaseLimitStatus);
    }

    @LogAuto(bizCode = 20090509, bizDesc = "抢单模式获取多个流水号的业务详细", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<BusinessView[]> getBusinessViews(String... businessId) throws ResponseException {
        BusinessView[] views = getGrabManagerService().getServiceViewByBusinessIds(businessId);
        return ResponseBack.success(views);
    }

    @Interceptor
    @LogAuto(bizCode = 20090510, bizDesc = "司机开始预约单向用户预约地点行驶", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> bespeakToBusiness(String driverId, String businessId) throws ResponseException {
        ResponseBack<Boolean> responseBack = getGrabManagerService().bespeakToBusiness(driverId, businessId);
        return responseBack;
    }

    @Interceptor
    @LogAuto(bizCode = 20090511, bizDesc = "用户在一定时间内允许取消预约单", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> bespeakCancel(String userId, String businessId) throws ResponseException {
        SpringConfigConstant springConfigConstant = this.getSpringConfigConstant();
        Long bespeakUserCancelValidateLimitSeconds = springConfigConstant.bespeakUserCancelValidateLimitSeconds();
        ResponseBack<Boolean> responseBack = getGrabManagerService().bespeakCancel(userId, businessId,
                bespeakUserCancelValidateLimitSeconds);
        if (responseBack.isSuccess() && !responseBack.getResult()) {
            //当前无系统错误,但预约取消不成功的时候,采用统一错误码处理
            responseBack = ResponseBack.error(this.get(RPC_USER_BESPEAK_NO_TIME_ERROR));
        }
        return responseBack;
    }

    @Override
    public ResponseBack<Integer> userCancelBespeakValidateLimitSeconds(int businessCode) throws ResponseException {
        SpringConfigConstant springConfigConstant = this.getSpringConfigConstant();
        Long bespeakUserCancelValidateLimitSeconds = springConfigConstant.bespeakUserCancelValidateLimitSeconds();
        return ResponseBack.success(bespeakUserCancelValidateLimitSeconds.intValue());
    }

    @Override
    public ResponseBack<Integer> userRequestBespeakMinLimitSeconds(int businessCode) throws ResponseException {
        SpringConfigConstant springConfigConstant = this.getSpringConfigConstant();
        Long userRequestBespeakMinLimitSeconds = springConfigConstant.userRequestBespeakMinLimitSeconds();
        return ResponseBack.success(userRequestBespeakMinLimitSeconds.intValue());
    }

    @Override
    public ResponseBack<Integer> userRequestBespeakMaxLimitSeconds(int businessCode) throws ResponseException {
        SpringConfigConstant springConfigConstant = this.getSpringConfigConstant();
        Long userRequestBespeakMaxLimitSeconds = springConfigConstant.userRequestBespeakMaxLimitSeconds();
        return ResponseBack.success(userRequestBespeakMaxLimitSeconds.intValue());
    }

    @Interceptor
    @LogAuto(bizCode = 20090512, bizDesc = "司机取消行程", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> driverCancelBusiness(String driverId, String businessId) throws ResponseException {
        ResponseBack<Boolean> responseBack = getGrabManagerService().driverCancelBusiness(driverId, businessId);
        return responseBack;
    }

    @Interceptor
    @LogAuto(bizCode = 20090513, bizDesc = "更改业务订单状态", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> modifyBusinessOrderInfo(OrderSimpleInfo info) throws ResponseException {
        try {
            LOGGER.info("业务订单状态修改->入参info={}", info);
            this.validateRequest(info);
            ResponseBack<Boolean> responseBack = getGrabManagerService().modifyBusinessOrderInfo(info);
            LOGGER.info("业务订单状态修改->修改数据状态={},businessId={}", responseBack.isSuccess(), info.getBusinessId());
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("业务订单状态修改->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Override
    public ResponseBack<List<BusinessCodeServiceStatus>> getServiceByRequestType(int requestType) throws
            ResponseException {
        try {
            UserRequestTypeInfo.UserRequestType type = UserRequestTypeInfo.UserRequestType.indexOf(requestType);
            if (type == null) {
                //非法参数
                return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
            }
            LOGGER.info("根据业务请求类别获取服务->requestType={}", requestType);
            ResponseBack<List<BusinessCodeServiceStatus>> responseBack = getGrabManagerService()
                    .getServiceByRequestType(type);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("根据业务请求类别获取服务->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090514, bizDesc = "分页查询未出行订单", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Pagination<BusinessServiceInfoVo>> quetyBusinessInfoByPage(PageVo vo) throws
            ResponseException {
        try {
            LOGGER.info("分页查询未出行订单->PageVo={}", vo);
            if (StringUtils.isBlank(vo.getUserId())) {
                return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
            }
            ResponseBack<Pagination<BusinessServiceInfoVo>> responseBack = getGrabManagerService()
                    .queryBusinessInfoByPage(vo);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("分页查询未出行订单->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090515, bizDesc = "查询订单抢单中的状态", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<EnableGrabBusinessStatus> queryEnableGrabBusinessStatus(String businessId) throws ResponseException {
        try {
            if (StringUtils.isBlank(businessId)) {
                return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
            }
            ResponseBack<EnableGrabBusinessStatus> responseBack = getGrabManagerService()
                    .queryEnableGrabBusinessStatus(businessId);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("查询订单抢单中的状态->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090516, bizDesc = "查询订单抢单中缓存信息", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<BusinessCacheVo> queryEnableGrabBusinessCache(String businessId) throws ResponseException {
        try {
            if (StringUtils.isBlank(businessId)) {
                return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
            }
            ResponseBack<BusinessCacheVo> responseBack = getGrabManagerService()
                    .queryEnableGrabBusinessCache(businessId);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("查询订单抢单中的状态->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取公共的facade
     * @since 1.0.3
     */
    CommonFacade getCommonFacade() {
        return this.facade("common", CommonFacade.class);
    }


    /**
     * @author Anson
     * @date 17-8-9
     * @name 获取执行器
     * @since 1.0.6
     */
    private ThreadPoolExecutor getThreadPoolExecutor() {
        return this.getService("standardThreadExecutor", ThreadPoolExecutor.class);
    }

    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取业务服务服务方法
     * @since 1.0.6
     */
    private BusinessServiceService getBusinessService() {
        return this.getService("businessServiceServiceImpl", BusinessServiceService.class);
    }

    /**
     * @author Anson
     * @date 17-8-9
     * @name 获取执行器
     * @since 1.0.6
     */
    private DynamicTaskManager getDynamicTaskManager() {
        return this.getService("springQuartzTaskManagerImpl", DynamicTaskManager.class);
    }

    /**
     * @author Anson
     * @date 17-8-9
     * @name 获取动态任务的服务管理
     * @since 1.0.6
     */
    private DynamicTaskService getDynamicTaskService() {
        return this.getService("redisDynamicTaskMapService", DynamicTaskService.class);
    }

    /**
     * @author Anson
     * @date 17-8-9
     * @name 获取抢单服务管理
     * @since 1.0.6
     */
    private GrabManagerService getGrabManagerService() {
        return this.getService("grabManagerServiceImpl", GrabManagerService.class);
    }

    /**
     * @author Anson
     * @date 17-8-9
     * @name 获取抢单缓存服务管理
     * @since 1.0.6
     */
    private GrabBusinessCacheService getGrabBusinessCacheService() {
        return this.getService("grabRedisBusinessCacheServiceImpl", GrabBusinessCacheService.class);
    }

    private GrabRedisBusinessWorkCacheService getGrabRedisBusinessWorkCacheService() {
        return this.getService("grabRedisBusinessWorkCacheServiceImpl", GrabRedisBusinessWorkCacheService.class);
    }

}
